package com.founder.bdyx.wxpro.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class HttpUtil {
    static Log log = LogFactory.getLog(HttpUtil.class);

    public static String sendGet(String url){
        try {
            log.info("http get请求路径：" + url);
            HttpGet request = new HttpGet(url);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpResponse response = httpClient.execute(request);

            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(response.getEntity(),"utf-8");
                log.info("http get请求返回内容：" + result);
                return result;
            }
            return "error," + response.getStatusLine().getStatusCode();
        }catch (Exception e){
            log.error("http get请求 发送失败：",e);
            return e.getMessage();
        }
    }

    /**
     * json格式传参
     * @param url
     * @param param
     * @return
     */
    public static String sendPost(String url, String param){
        try{
            log.info("http post请求路径：" + url);
            log.info("http post请求参数: " + param);
            HttpPost post = new HttpPost(url);
            CloseableHttpClient client = HttpClients.createDefault();
            StringEntity entity = new StringEntity(param, "UTF-8");
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            post.setEntity(entity);
            HttpResponse resp = client.execute(post);
            if (resp.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(resp.getEntity(), "utf-8");
//                Map<String, Object> resMap = JSON.parseObject(result);
                return result;
            }
            return "error," + resp.getStatusLine().getStatusCode();
        }catch(Exception e){
            log.error("http post请求 发送失败：" + e);
            return e.getMessage();
        }
    }

    /**
     * 模拟表单form-data格式传参
     * @param url
     * @param params
     * @return
     */
    public static String sendPostForm(String url, JSONObject params){
        try{
            log.info("http post请求路径：" + url);
            log.info("http post请求参数: " + params);
            HttpPost post = new HttpPost(url);
            CloseableHttpClient client = HttpClients.createDefault();

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            Set<String> keySet = params.keySet();
            for (String key : keySet) {
                nvps.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
            }
            post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

            HttpResponse resp = client.execute(post);
            if (resp.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(resp.getEntity(), "utf-8");
//                Map<String, Object> resMap = JSON.parseObject(result);
                return result;
            }
            return "{\"code\": "+resp.getStatusLine().getStatusCode()+",\"msg\": \"审核失败！\"}";
        }catch(Exception e){
            log.error("http post请求 发送失败：" + e);
            return "{\"code\": 0,\"msg\": \"请求异常："+e.getMessage()+"\"}";
        }
    }

    public static void main(String[] args) {
        String retStr = HttpUtil.sendGet("http://111.111.119.147:8080/nis7/bk001Sbk/f_json/checkReportCard.shtml?id=000197583500_1&ticket=nis&employeeId=00000&icds=J10.101|A16.202&ptype=zy&verifyMode=0");
        JSONObject retJson = JSONObject.parseObject(retStr);
        System.out.println(retJson);
    }
}
